#ifndef AUTHMANAGER_H
#define AUTHMANAGER_H

#include "entity/SysUser.h"
#include <QObject>
#include <QReadWriteLock>
#include "singleton.h"

class AuthManager : public QObject
{
    Q_OBJECT
    Q_PROPERTY(QVariantMap user READ userMap NOTIFY userChanged)
    Q_PROPERTY(bool authenticated READ isAuthenticated NOTIFY authChanged)
public:
    // static AuthManager& instance();
    SINGLETON(AuthManager)
    ~AuthManager() override;
    QVariantMap userMap() const;
    // 用户认证操作
    void login(const SysUser& userInfo);
    void logout();
    bool isAuthenticated() const;
    SysUser currentUser() const;
    // 获取访问令牌
    QString accessToken() const;
    // 刷新令牌
    void refreshToken();

signals:
    void authChanged(bool authenticated);
    void userChanged();
    void tokenExpired();

private:
    explicit AuthManager(QObject* parent = nullptr);
    AuthManager(const AuthManager&) = delete;
    AuthManager& operator=(const AuthManager&) = delete;
    QString username() const;
    SysUser m_currentUser;
    QString m_accessToken;
    QString m_refreshToken;
    QDateTime m_tokenExpiry;
    bool m_authenticated = false;
    mutable QReadWriteLock m_lock; // 线程安全锁
};

#endif // AUTHMANAGER_H
